package com.example.kt_temp.ui.viewmodel

import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.example.kt_temp.model.entity.ArticleEntity
import com.example.kt_temp.model.service.ArticleService

class ArticleViewModel:ViewModel() {

    private val articleService = ArticleService.instance()

    private var pageSize =10
    private var pageOffset=1

    //文章新闻列表数据
     var list by mutableStateOf(
        listOf(
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),
            ArticleEntity(
                title="仁沙士大夫拉萨路费，浪费，大厦",
                source = "大厦考虑到",
                timestamp = "2022-2-2"
            ),

            )
     )
        private set

    var listLoaded by mutableStateOf(false)
        private set

    //下拉刷新
    var refreshing by mutableStateOf(false)
        private set

    //是否还有更多
    private var hasMore = true

    suspend fun fetchArticleList(){
        val res =  articleService.list(pageOffset=pageOffset,pageSize =pageSize)
        if(res.code == 0 && res.data!=null){
            val tempList = mutableListOf<ArticleEntity>()
            if(pageOffset !=1){
                tempList.addAll(list)
            }
            tempList.addAll(res.data)
            hasMore = res.data.size == pageSize
            list =tempList
            listLoaded = true
            refreshing =false
        }else{
            pageOffset--
            if(pageOffset<=1){
                pageOffset =1
            }
        }
    }

    //下拉刷新
    suspend fun refresh(){
        pageOffset = 1
        listLoaded =false
        fetchArticleList()
    }

    suspend fun loadMore(){
        if(hasMore){
            pageOffset++
            fetchArticleList()
        }

    }



    //HTML 头部
    private val htmlHeader = """
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title></title>
            <style>
                img {
                    max-width: 100% !important;
                }
            </style>
        </head>
        <body>
    """.trimIndent()

    //html尾部
    private val htmlFooter = """
        </body>
        </html>
    """.trimIndent()

    private var articleEntity:ArticleEntity? =null

    var content by mutableStateOf(
        """$htmlHeader
        ${articleEntity?.content?:""}
        $htmlFooter
    """.trimIndent()
    )

    var infoLoaded by mutableStateOf(false)
        private set

    suspend fun fetchInfo(){
        val res = articleService.info("")
        if(res.code == 0 && res.data!=null) {
            articleEntity = res.data
            content = """$htmlHeader
                    ${articleEntity?.content ?: ""}
                    $htmlFooter
                """.trimIndent()
            infoLoaded =true
        }
    }


}